import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要扫描的路径：");
        String scanPath = sc.next();
        File file = new File(scanPath);
        if(!file.isDirectory()){
            return;
        }
        System.out.println("请输入要查找的名称：");
        String target = sc.next();
        List<File> ans = new ArrayList<>();
        dfs(file, target, ans);
        for(File e : ans){
            System.out.println(e.getCanonicalFile());
        }
    }

    private static void dfs(File file, String target, List<File> ans) throws FileNotFoundException {
        File[] files = file.listFiles();
        for(int i = 0; i < files.length; i++){
            if(files[i].isDirectory()){
                dfs(files[i], target, ans);
            }else{
                StringBuilder sb = new StringBuilder();
                InputStream is = new FileInputStream(files[i]);
                Scanner scanner = new Scanner(is, "UTF-8");
                while(scanner.hasNext()){
                    sb.append(scanner.nextLine());
                }
                String s = sb.toString();
                if(s.contains(target)){
                    ans.add(files[i]);
                }
            }
        }
    }
}
